package com.example.onlineeducation.ui.topic

import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import com.example.onlineeducation.App
import com.example.onlineeducation.BuildConfig
import com.example.onlineeducation.R
import com.example.onlineeducation.base.BaseFragment
import com.example.onlineeducation.data.constant.MaterialType
import com.example.onlineeducation.data.entity.ChapterEntity
import com.example.onlineeducation.databinding.StudyMaterialFragmentBinding
import com.example.onlineeducation.ui.common.CommonImgZoomActivity
import com.example.onlineeducation.viewmodel.StudyContentVM
import com.example.zhengsqdemo.weight.VideoUtil
import com.yc.jetpacklib.extension.ycIsNotEmpty
import com.yc.jetpacklib.extension.ycLoadImageNet
import com.yc.jetpacklib.net.doFail
import com.yc.jetpacklib.net.doSuccess
import com.yc.jetpacklib.widget.dialog.YcCommonDialog

class StudyMaterialFragment(private val topicId: String) :
    BaseFragment<StudyMaterialFragmentBinding>(StudyMaterialFragmentBinding::inflate) {
    private val mViewModel: StudyContentVM by activityViewModels()
    private lateinit var mVideoUtil: VideoUtil
    private var currentChapterIndex = 0//当前素材位置
    private var mChapterEntityList: List<ChapterEntity>? = null

    private val mDialog: YcCommonDialog by lazy {
        YcCommonDialog(this.requireContext(), this)
    }

    override fun StudyMaterialFragmentBinding.initView(view: View, savedInstanceState: Bundle?) {
        mVideoUtil = VideoUtil(requireActivity(), this@StudyMaterialFragment, videoView)

        mViewModel.mGetChapterListDataModel.observe {
            it.doSuccess { data ->
                if (data.ycIsNotEmpty()) {
                    mChapterEntityList = data
                    currentChapterIndex = 0
                    initStudyView()
                    mViewModel.studyTimeLoop(true)
                } else {
                    mDialog.setMsg("无学习素材").setSingleBtnText("确定").setSingleBtnText("确定")
                        .setSingleOnClick {
                            requireActivity().finish()
                        }.showSingle()
                }
            }.doFail {
                mDialog.setMsg(it.msg).setSingleBtnText("确定").setSingleBtnText("确定")
                    .setSingleOnClick {
                        requireActivity().finish()
                    }.showSingle()
            }
        }

        tvPageNext.setOnClickListener {
            currentChapterIndex++
            initStudyView()
        }
        tvPagePrevious.setOnClickListener {
            currentChapterIndex--
            initStudyView()
        }
        mViewModel.getChapterList(App.mInstant.mViewModel.mProInfoEntity.value!!.proId, topicId)
    }

    private fun StudyMaterialFragmentBinding.initStudyView() {
        val currentChapter = mChapterEntityList!![currentChapterIndex]
        (activity as StudyContentActivity).mChapterId = currentChapter.id
        mVideoUtil.closeVideo()
        tvMaterialTitle.text = currentChapter.chapterName
        tvMaterialContent.text = currentChapter.chapterContent
        when (currentChapter.chapterType) {
            MaterialType.VIDEO -> {
                //mVideoUtil.setUrl("http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4")
                if (currentChapter.chapterMediaUrl.ycIsNotEmpty()) {
                    mVideoUtil.setUrl(currentChapter.chapterMediaUrl?:"")
                    videoView.visibility = View.VISIBLE
                    tvMaterialEmpty.visibility = View.GONE
                }else{
                    tvMaterialEmpty.visibility = View.VISIBLE
                    videoView.visibility = View.GONE
                }
                ivMaterialImage.visibility = View.GONE
                ivVoice.visibility = View.VISIBLE
                tvMaterialTitle.visibility = View.VISIBLE
                tvMaterialContent.visibility = View.VISIBLE
            }
            MaterialType.IMAGE -> {
                videoView.visibility = View.GONE
                ivVoice.visibility = View.GONE
                tvMaterialTitle.visibility = View.GONE
                tvMaterialContent.visibility = View.GONE
                if (currentChapter.chapterMediaUrl.ycIsNotEmpty()) {
                    ivMaterialImage.visibility = View.VISIBLE
                    tvMaterialEmpty.visibility = View.GONE
                    //ivMaterialImage.ycLoadImageNet(currentChapter.chapterMediaUrl,errorImg = R.drawable.ic_topic)
                    ivMaterialImage.apply {
                        enable()
                        this.ycLoadImageNet(currentChapter.chapterMediaUrl)
                    }
                } else {
                    tvMaterialEmpty.visibility = View.VISIBLE
                    ivMaterialImage.visibility = View.GONE
                }
            }
        }

        if (currentChapterIndex > 0) {
            tvPagePrevious.visibility = View.VISIBLE
        } else {
            tvPagePrevious.visibility = View.GONE
        }
        if (currentChapterIndex < mChapterEntityList!!.size - 1) {
            tvPageNext.visibility = View.VISIBLE
        } else {
            tvPageNext.visibility = View.GONE
        }

//        ivMaterialImage.setOnClickListener {
//            CommonImgZoomActivity.newInstance(requireContext(),currentChapter.chapterMediaUrl)
//        }
    }

    fun onBackPressed(): Boolean {
        return mVideoUtil.backFullToBackPressed()
    }

}
